package servlet;

import entity.Group;
import entity.User;
import groupInformation.Relation;
import serviceImpl.add;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

/**添加小组功能
 * @author 祁
 * @time 6.11/15:50
 * */

@WebServlet(name = "JoinGroupServlet", value = "/JoinGroupServlet")
public class JoinGroupServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out=response.getWriter();
        User user =(User) request.getSession().getAttribute("user");
        String id=request.getParameter("id");
        int ID=Integer.parseInt(id);
        Group g=new Group();
        g.setId(ID);

        add a=new add();
        boolean t = a.add(g, user);
        String text="";
        if(t==true){
            Relation relation=new Relation();
            relation.setGroup_id(ID);



            request.getSession().setAttribute("group",g);
            System.out.println();
             text="加入成功！";

        } else{
            text="不存在这样的小组！";
        }
        out.print(text);
    }
}
